package view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.laoxinwen.app.R;

import java.util.ArrayList;
import java.util.List;

import bean.MainNewsTab;

/**
 * Created by sshong on 15/6/16.
 */
public class MainTopTabContainer extends HorizontalScrollView implements View.OnClickListener {

    private List<MainNewsTab> tabDatas;
    private MainNewsTab selectedTab;

    public MainTopTabContainer(Context context) {
        super(context);
    }

    public MainTopTabContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MainTopTabContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MainTopTabContainer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private LinearLayout getTabContainer() {
        LinearLayout tabContainer = (LinearLayout) getChildAt(0);
        return tabContainer;
    }

    /**
     * 设置newstab数组
     *
     * @param tabs
     */
    public void setTabs(MainNewsTab[] tabs) {
        LinearLayout tabContainer = getTabContainer();
        tabContainer.removeAllViews();
        selectedTab = null;

        tabDatas = new ArrayList<MainNewsTab>();
        for (MainNewsTab newsTab : tabs) {
            MainTopTab tab = (MainTopTab) LayoutInflater.from(getContext()).inflate(R.layout.main_toptab_item, tabContainer, false);
            tab.setData(newsTab);
            tab.setOnClickListener(this);
            tabContainer.addView(tab);
            tabDatas.add(newsTab);
        }

        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        int spec = new View.MeasureSpec().makeMeasureSpec(screenWidth, View.MeasureSpec.EXACTLY);
        measure(spec, spec);
        if (tabContainer.getMeasuredWidth() < getMeasuredWidth()) {
            int extraPadding = (getMeasuredWidth() - tabContainer.getMeasuredWidth()) / (tabContainer.getChildCount() * 2);
            for (int i = 0; i < tabContainer.getChildCount(); i++) {
                View v = tabContainer.getChildAt(i);
                v.setPadding(v.getPaddingLeft() + extraPadding, v.getPaddingTop(), v.getPaddingRight() + extraPadding, v.getPaddingBottom());
            }
            measure(spec, spec);
        }

        if (tabs.length != 0) {
            selectTab(tabs[0]);
        }
    }

    /**
     * 选择某tab
     *
     * @param tab
     */
    public void selectTab(MainNewsTab tab) {
        LinearLayout tabContainer = getTabContainer();
        if (selectedTab != tab) {
            int index = tabDatas.indexOf(tab);
            View selectedChild = tabContainer.getChildAt(index);
            selectedChild.setSelected(true);

            if (selectedTab != null) {
                index = tabDatas.indexOf(selectedTab);
                tabContainer.getChildAt(index).setSelected(false);
            }

            selectedTab = tab;
            //scroll
            if (getMeasuredWidth() < tabContainer.getMeasuredWidth()) {
                if (selectedChild.getRight() > getMeasuredWidth() + getScrollX()) {
                    smoothScrollTo(selectedChild.getRight() - getMeasuredWidth(), 0);
                }
                if (selectedChild.getLeft() < getScrollX()) {
                    smoothScrollTo(selectedChild.getLeft(), 0);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v instanceof MainTopTab) {
            MainTopTab vtab = (MainTopTab) v;
            MainNewsTab tab = vtab.getData();
            selectTab(tab);
            // 仅在点击选择时触发
            try {
                OnTopTabSelectHandler handler = (OnTopTabSelectHandler) getContext();
                handler.onSelectTopTab(tab, tabDatas.indexOf(tab));

            } catch (ClassCastException e) {
                throw new ClassCastException(getContext().toString()
                        + " must implement OnTopTabSelectHandler");
            }
        }
    }

    public interface OnTopTabSelectHandler {
        public void onSelectTopTab(MainNewsTab tab, int index);
    }
}
